package com.crm.business.admin.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.crm.business.admin.log.AdminDeptLog;
import com.crm.business.admin.service.IAdminDeptService;
import com.crm.common.admin.entity.BO.AdminDeptBO;
import com.crm.common.admin.entity.BO.AdminDeptQueryBO;
import com.crm.common.admin.entity.VO.AdminDeptVO;
import com.crm.common.ka.core.common.ApiExplain;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.common.SubModelType;
import com.crm.common.ka.core.common.log.BehaviorEnum;
import com.crm.common.ka.core.common.log.SysLog;
import com.crm.common.ka.core.common.log.SysLogHandler;
import com.crm.common.ka.core.entity.UserInfo;
import com.crm.common.ka.core.feign.admin.entity.SimpleDept;
import com.crm.business.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-04-27
 */
@RestController
@RequestMapping("/adminDept")
@Api(tags = "部门管理相关接口")
@SysLog(subModel = SubModelType.ADMIN_DEPARTMENT_MANAGEMENT,logClass = AdminDeptLog.class)
public class AdminDeptController {

    @Autowired
    private IAdminDeptService adminDeptService;

//    @Value("${crm.franchisee.deptId}")
//    private String deptId;


    @PostMapping("/queryDeptTree")
    @ApiOperation("查询部门列表树")
    public Result<List<AdminDeptVO>> queryDeptTree(@RequestBody AdminDeptQueryBO queryBO) {
        /*if (!UserUtil.isAdmin()){
            UserInfo userInfo=UserUtil.getUser();
            Long pid=adminDeptService.queryPidByDeptId(Long.valueOf(userInfo.getDeptId()));
            queryBO.setId(Math.toIntExact(pid));
        }*/
        List<AdminDeptVO> pidList = adminDeptService.queryDeptTree(queryBO);
        List<AdminDeptVO> deptList =new ArrayList<>();
        UserInfo userInfo = UserUtil.getUser();
        String deptName= userInfo.getDeptName();
        // 加盟商组织查看本部门及下属部门
//        List<Integer> deptIds = this.adminDeptService.queryChildDept(Integer.valueOf(deptId));
//        deptIds.add(Integer.valueOf(deptId));

        if(!UserUtil.isAdmin()){
//            if(!deptIds.contains(userInfo.getDeptId())){
//                return Result.ok(pidList);
//            }
            for (int i=0;i<pidList.size();i++){
                if (pidList.get(i).getName().equals(deptName)){
                    deptList.add(pidList.get(i));
                }
            }
            return Result.ok(deptList);
        }else {
            return Result.ok(pidList);
        }

    }

    @PostMapping("/addDept")
    @ApiOperation("新增部门")
    @SysLogHandler(behavior = BehaviorEnum.SAVE,object = "#adminDept.name",detail = "'添加了部门:'+#adminDept.name")
    public Result addDept(@RequestBody @Valid AdminDeptBO adminDept) {
        adminDeptService.addDept(adminDept);
        return Result.ok();
    }

    @PostMapping("/setDept")
    @ApiOperation("修改部门")
    @SysLogHandler(behavior = BehaviorEnum.SAVE,object = "#adminDept.name",detail = "'修改了部门:'+#adminDept.name")
    public Result setDept(@RequestBody @Valid AdminDeptBO adminDept) {
        adminDeptService.setDept(adminDept);
        return Result.ok();
    }

    @PostMapping("/deleteDept/{deptId}")
    @ApiOperation("删除部门")
    @SysLogHandler(behavior = BehaviorEnum.DELETE)
    public Result deleteDept(@PathVariable("deptId") Integer deptId) {
        adminDeptService.deleteDept(deptId);
        return Result.ok();
    }

    @RequestMapping("/getNameByDeptId")
    @ApiExplain("根据部门ID获取部门名称")
    public Result getNameByDeptId(Integer deptId) {
        return R.ok(adminDeptService.getNameByDeptId(deptId));
    }

    @RequestMapping("/queryChildDeptId")
    @ApiExplain("根据部门ID下的子部门")
    public Result<List<Integer>> queryChildDeptId(@NotNull Integer deptId) {
        return R.ok(adminDeptService.queryChildDept(deptId));
    }

    @PostMapping("/queryDeptByIds")
    @ApiExplain("根据部门ID获取用户")
    public Result<List<SimpleDept>> queryDeptByIds(@RequestBody List<Integer> ids) {
        List<SimpleDept> simpleDepts = adminDeptService.queryDeptByIds(ids);
        return R.ok(simpleDepts);
    }

    @RequestMapping("/queryDeptByPid")
    @ApiExplain("根据部门ID下一级部门")
    public Result<List<Integer>> queryDeptByPid(@NotNull Integer deptId) {
        return R.ok(adminDeptService.queryDeptByPid(deptId));
    }

    @PostMapping("/queryAllDeptNameByDeptId")
    @ApiExplain("根据组织Id查询所有上级组织名称")
    public Result<String> queryAllDeptNameByDeptId(Integer deptId){
        return R.ok(adminDeptService.queryAllDeptNameByDeptId(deptId));
    }


    @PostMapping("/queryEndOfDeptTree")
    @ApiOperation("查询所有末级部门列表树")
    public Result<List<AdminDeptVO>> queryEndOfDeptTree(@RequestBody AdminDeptQueryBO queryBO) {
        List<AdminDeptVO> pidList = adminDeptService.queryDeptTree(queryBO);
        List<AdminDeptVO> deptList =new ArrayList<>();
        UserInfo userInfo = UserUtil.getUser();
        String deptName= userInfo.getDeptName();
        // 加盟商组织查看本部门及下属部门
//        List<Integer> deptIds = this.adminDeptService.queryChildDept(Integer.valueOf(deptId));
//        deptIds.add(Integer.valueOf(deptId));

        if(!UserUtil.isAdmin()){
//            if(!deptIds.contains(userInfo.getDeptId())){
//                deptList = pidList;
//            }
            for (int i=0;i<pidList.size();i++){
                if (pidList.get(i).getName().equals(deptName)){
                    deptList.add(pidList.get(i));
                }
            }
            return Result.ok(deptList);
        }else {
            deptList = pidList;
        }

        if(CollectionUtil.isNotEmpty(deptList)){
            List<Integer> pids = deptList.stream().map(AdminDeptVO::getPid).collect(Collectors.toList());
            List<AdminDeptVO> result = new ArrayList<>();
            for(AdminDeptVO vo : deptList){
                if(!pids.contains(vo.getDeptId())){
                    result.add(vo);
                }
            }
            return Result.ok(result);
        }
        return Result.ok(deptList);


    }
}

